//
//  FH_DUApiTool.m
//  APIManagerProject
//
//  Created by HeTaiQiang on 15/11/17.
//  Copyright © 2017年 HeTaiQiang. All rights reserved.
//

#import "FH_DUApiTool.h"
#import <CommonCrypto/CommonDigest.h>

@implementation FH_DUApiTool

+ (NSDictionary *)handleRequestWithParameters:(NSDictionary *)parameters {
    
    //如果没有参数不用处理
    if (parameters.allKeys.count==0) {
        return nil;
    }
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:parameters];
    //房子ID
//    if ([UserItemModel sharedModel].getAccount.houseid) {
//        [dict setObject:[UserItemModel sharedModel].getAccount.houseid forKey:@"houseid"];
//    }else{
//        [dict setObject:@"0" forKey:@"houseid"];
//    }
    //新C版小区ID写死为3
    [dict setObject:@3 forKey:@"villageid"];
    
    
    NSMutableString *signString = [NSMutableString new];
    
    //1.取到所有的Key,对Key进行排序
    NSArray *allKeys = dict.allKeys;
    allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *key in allKeys) {
        
        NSString *value = dict[key];
        [signString appendString:[NSString stringWithFormat:@"%@",value]];
    }
    
    //2.拼接字符串Fanghui2016,并md5
    [signString appendString:@"Fanghui2016"];
    NSString *sign = [self md532BitWithStr:signString];
    
    //3.构建新的请求参数
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dict];
    [dic setObject:sign forKey:@"sign"];
    //将字典转化为json字符串
    NSString *jsonString = [self  jsonEncodedKeyValueStringWithDict:dic];
    
    return @{@"json":jsonString};
    
    return parameters;
}

+ (NSString *)handleRequestGetWithParameters:(NSDictionary *)parameters {
    
    //如果没有参数不用处理
    if (parameters.allKeys.count==0) {
        return nil;
    }
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:parameters];
    //房子ID
    //    if ([UserItemModel sharedModel].getAccount.houseid) {
    //        [dict setObject:[UserItemModel sharedModel].getAccount.houseid forKey:@"houseid"];
    //    }else{
    //        [dict setObject:@"0" forKey:@"houseid"];
    //    }
    //新C版小区ID写死为3
    [dict setObject:@3 forKey:@"villageid"];
    
    NSMutableString *signString = [NSMutableString new];
    
    //1.取到所有的Key,对Key进行排序
    NSArray *allKeys = parameters.allKeys;
    allKeys = [allKeys sortedArrayUsingSelector:@selector(compare:)];
    for (NSString *key in allKeys) {
        
        NSString *value = parameters[key];
        [signString appendString:[NSString stringWithFormat:@"%@",value]];
    }
    
    //2.拼接字符串Fanghui2016,并md5
    [signString appendString:@"Fanghui2016"];
    
    NSLog(@"md5加密—%@—————————%@——",allKeys,signString);
    
    NSString *sign = [self md532BitWithStr:signString];
    
    //3.构建新的请求参数
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:parameters];
    [dic setObject:sign forKey:@"sign"];
    
    //将字典转化为json字符串
    NSString *jsonString = [self  jsonEncodedKeyValueStringWithDict:dic];
    
    
    return [NSString stringWithFormat:@"?json=%@", jsonString];
}


/**
 把字典转换成字符串

 @param dictionary 参数字典
 @return 字符串
 */
+ (NSString *)jsonEncodedKeyValueStringWithDict:(NSDictionary *)dictionary {
    
    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
                                                   options:0 // non-pretty printing
                                                     error:&error];
    if(error) {
        
        NSLog(@"JSON Parsing Error: %@", error);
    }

    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}

/**
 *  MD5编码32位
 *
 *  @param str 需要编码的字符串
 *
 *  @return 编码结束的字符串
 */
+ (NSString *)md532BitWithStr:(NSString *)str
{
    
    const char *cStr = [str UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), result ); // This is the md5 call
    
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

@end
